/**
 * name: mabel xu
 * date: april 11, 2008
 * filename: HashTable.java
 * description: implements actual HashTable (using ArrayLists and holding Items)
 */

import java.util.*;

public class HashTable
{
	private int size;
	private int capacity;
	private LinkedList[] table;

	public HashTable()
	{
		size = 0;
		capacity = 700;
		table = new LinkedList[700];
	}

	public HashTable(int numSlots)
	{
		size = 0;
		capacity = numSlots;
		table = new LinkedList[capacity];
	}

	public int getSize()
	{
		return size;
	}

	public int getCapacity()
	{
		return capacity;
	}

	public void add(Object obj)
	{
		Item i = (Item) obj;
		int index = this.hash(i.hashCode());
		if (table[index] == null)
			table[index] = new LinkedList();
		table[index].add(i);
		size++;
	}

	public Object find(Comparable target)
	{
		Item t = (Item) target;
		int index = this.hash(t.hashCode());
		if (table[index] == null)
			return null;

		Iterator iter = table[index].iterator();
		while (iter.hasNext())
		{
			Item current = (Item)(iter.next());
			if (current.equals(t))
				return (Object)current;
		}

		return null;
	}

	private int hash(int hashCode)
	{
		return hashCode % capacity;
	}
}